/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.util;

import java.nio.IntBuffer;

import com.jme.util.geom.BufferUtils;

public class LODUtils {

	public static long[] squareDistances(int[] distances) {
		long[] ret = new long[distances.length];
		for (int k=0; k < distances.length; k++)
			ret[k] = distances[k]*distances[k];		
		return ret;
	}
	
	public static Integer[] sequence(int max) {
		Integer[] ret = new Integer[max+1];
		for (int k=0; k < max+1; k++)
			ret[k] = new Integer(k);
		return ret;
	}
	
	
	
	public static int[] generateBuffer(int blocksize, int detail, boolean top, boolean bottom, boolean left, boolean right) {
		int eblock = blocksize + 1;
		
		if (blocksize % detail != 0) {
			System.out.println("Too many detail levels for this block size!");
			System.exit(0);
		}
		
		int value = ((blocksize / detail) * (blocksize / detail) * 6);
		
		int penalty = ((blocksize/detail)/2)*3;
		
		if (top)
			value-=penalty;
		
		if (bottom)
			value-=penalty;
		
		if (left)
			value-=penalty;
		
		if (right)
			value-=penalty;	
		
		
		int[] indices = new int[value];

		int count = 0;
		
		int one1, one2, one3, two1, two2, two3;
		boolean one, two;
		// go through entire array up to the second to last column.
		for (int x = 0; x < blocksize; x += detail) {
			for (int y = 0; y < blocksize; y += detail) {
				one = true;
				two = true;
				
				
				one1 = x + (y * eblock); // top left
				one2 = x + detail + (eblock * (y + detail)); // bottom right
				one3 = x + detail + (y * eblock); //top right
				

				two1 = x + (y * eblock); // top left
				two2 = x + (eblock * (y + detail));// bottom left
				two3 = x + detail + (eblock * (y + detail)); // bottom right
				
				if (top && y == 0) {
					if ((x / detail) % 2 == 0) {
						// top right +1x
						one3+=detail;
					}
					else {
						one = false;
						two3 = two2;
						two1 = one2;
						two2 = one3;												 
					}
				}
				
				if (bottom && y == blocksize - detail) {
					if ((x / detail) % 2 == 0) {
						two = false;
						one2 = two2;
					}
					else {
						// bottom left - 1x;
						two2-=detail;
					}
				}
				
				if (left && x==0) {
					if (y / detail % 2 == 0) {
						// bottom left + 1y
						two2 += (eblock * detail);
					}
					else {
						one = false;
						two1 = one3;
					}
				}
				
				if (right && x==blocksize - detail) {
					if (y / detail % 2 == 0) {
						two = false;
						one2 = two2;
					}
					else {
						one3-=(eblock * detail);
					}
				}				

				if (one) {
					indices[count++] = one1;
					indices[count++] = one2;
					indices[count++] = one3;
				}
				
				if (two) {
					indices[count++] = two1;
					indices[count++] = two2;
					indices[count++] = two3;
				}
				
// if (x==0 && y == 0) {
// // set the top left corner.
//					indices[count++] = x + (y * eblock);
//					// set the bottom right corner.
//					indices[count++] = x + detail + (eblock * (y + detail));
//					
//					// set the top right corner.
//					indices[count++] = x + detail + detail +(y * eblock);
//					
//					
//					// set the top left corner
//					indices[count++] = x + (y * eblock);
//					// set the bottom left corner
//					indices[count++] = x + (eblock * (y + detail));
//					// set the bottom right corner
//					indices[count++] = x + detail + (eblock * (y + detail));
//					continue;
//				}
//				
//				if (x==detail && y == 0) {
//////					 set the top left corner.
////					indices[count++] = x + (y * eblock);
////					// set the bottom right corner.
////					indices[count++] = x + detail + (eblock * (y + detail));
////					// set the top right corner.
////					indices[count++] = x + detail + 1 +(y * eblock);
//					// set the bottom left corner
//					indices[count++] = x + (eblock * (y + detail));
//					// set the bottom right corner
//					indices[count++] = x + detail + (eblock * (y + detail));
//					// set the top right corner
//					indices[count++] = x + detail + (y * eblock);
//
//					continue;
//				}
//				
//				// set the top left corner.
//				indices[count++] = x + (y * eblock);
//				// set the bottom right corner.
//				indices[count++] = x + detail + (eblock * (y + detail));
//				// set the top right corner.
//				indices[count++] = x + detail + (y * eblock);
//				// set the top left corner
//				indices[count++] = x + (y * eblock);
//				// set the bottom left corner
//				indices[count++] = x + (eblock * (y + detail));
//				// set the bottom right corner
//				indices[count++] = x + detail + (eblock * (y + detail));
			}
		}
		return indices;
	}
	
	private static final int extra = 1 + 4 + 4 + 2 + 4 + 1; //   + | + |_ + | | + |_| + |=|
	public static IntBuffer getIndicis(int detail, IntBuffer[] indices, boolean top, boolean bottom, boolean left, boolean right) {
		int ret = detail * extra;
		
		if (top)
			ret+=8;
		if (bottom)
			ret+=4;
		if (left)
			ret+=2;
		if (right)
			ret+=1;
		
		return indices[ret];
	}
	
	public static IntBuffer[] generateIndicis(int blocksize, int num) {
		int detail = 1;
		
		IntBuffer[] ret = new IntBuffer[(num * extra)+1];
		int bufcount = 0;
		
		
		
		for (int k=0; k < num+1; k++) {
			
			
			ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, false, false, false));
			if (k!=num) {

				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, false, false, true));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, false, true, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, false, true, true));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, true, false, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, true, false, true));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, true, true, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, false, true, true, true));

				
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, false, false, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, false, false, true));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, false, true, false));	
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, false, true, true));
				
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, true, false, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, true, false, true));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, true, true, false));
				ret[bufcount++] = BufferUtils.createIntBuffer(generateBuffer(blocksize, detail, true, true, true, true));
				
				
			}
			
			detail+=detail;
		}
		return ret;
	}
	
}
